// 实现这个项目的构建任务

// 引入gulp模块
const { src, dest, parallel, series, watch } = require('gulp')
// 引入清除文件模块
const del = require('del')

// 自动加载gulp内所有插件
const gulpLoadPlugins = require('gulp-load-plugins')
const plugins = gulpLoadPlugins()

// 加载开发服务器
const browserSync = require('browser-sync')
const bs = browserSync.create()

// HTML模板需要用到的数据
const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

// 清除文件方法
const clean = () => {
  return del(['temp', 'dist'])
}

// 创建sass编译任务
const style = () => {
  return src('src/assets/styles/*.scss', { base: 'src' })
    .pipe(plugins.sass({ outputStyle: 'expanded' }))
    .pipe(dest('temp'))
    .pipe(bs.reload({ stream: true }))
}

// 创建html模板编译任务
const page = () => {
  return src('src/*.html', { base: 'src' })
    .pipe(plugins.swig({ data, defaults: { cache: false } }))
    .pipe(dest('temp'))
    .pipe(bs.reload({ stream: true }))
}

// 创建js编译任务
const script = () => {
  return src('src/assets/scripts/*.js', { base: 'src' })
    .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
    .pipe(dest('temp'))
    .pipe(bs.reload({ stream: true }))
}

// 图片 字体 以及一些静态资源 只需放到dist(线上)目录下
// 图片压缩转换
const image = () => {
  return src('src/assets/images/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

// 字体压缩转换
const font = () => {
  return src('src/assets/fonts/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

// public下静态资源
const extra = () => {
  return src('public/**', { base: 'public' })
    .pipe(dest('dist'))
}

// 上线前准备
const useref = () => {
  return src('temp/*.html', { base: 'temp' })
    // 解决打包时引用node_module下资源找不到问题
    .pipe(plugins.useref({ searchPath: ['temp', '.'] }))
    // 使用uglify压缩js
    .pipe(plugins.if(/\.js$/, plugins.uglify()))
    // 使用clean-css压缩css
    .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
    // 使用htmlmin压缩html
    .pipe(plugins.if(/\.html$/, plugins.htmlmin({ 
      collapseWhitespace: true, // 折叠所有空白字符
      minifyCSS: true, // 压缩行内css
      minifyJS: true  // 压缩行内js
    })))
    .pipe(dest('dist'))
}

// 启动开发服务器
const openBrowser = () => {
  // 监听文件变化执行构建任务
  watch('src/assets/styles/*.scss', style)
  watch('src/*.html', page)
  watch('src/assets/scripts/*.js', script)
  watch([
    'public/**',
    'src/fonts/**',
    'src/images/**'
  ], bs.reload)
  
  bs.init({
    notify: false,
    port: '3080',
    server: {
      baseDir: ['temp', 'src', 'public'],
      routes: {
        '/node_modules': 'node_modules'
      }
    }
  })
}


// 开发任务
const compie = parallel(style, page, script)
// 开发组合任务
const server = series(compie, openBrowser)
// 上线前打包组合任务
const build = series(clean, parallel(compie, image, font, extra), useref)

module.exports = {
  clean,
  build,
  server
}